<?php

class ErrorController extends Zend_Controller_Action
{

    public function errorAction()
    {
        $errors = $this->_getParam('error_handler');
        
        if (!$errors) {
            $this->view->message = 'You have reached the error page';
            return;
        }
        
        switch ($errors->type) {
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
        
                // 404 error -- controller or action not found
                $this->getResponse()->setHttpResponseCode(404);
                $this->view->message = 'Page not found';
                
				////如果在前台找不到頁面，就導頁到前台的首頁。
                if('default' === $this->_getParam('module'))
                {
					$this->_redirect('');
                }
				////如果在後台找不到頁面，就導頁到後台的首頁。
				elseif('administrator' === $this->_getParam('module'))
				{ 
					$this->_redirect($this->_getParam('module'));
				}
				else
				{
					$this->_redirect();
				}
                
                break;
            default:
                // application error
                $this->getResponse()->setHttpResponseCode(500);
                $this->view->message = 'Application error';
                break;
        }
        
        // Log exception, if logger available
        if ($log = $this->getLog()) {
            $log->crit($this->view->message, $errors->exception);
        }
        
        // conditionally display exceptions
        if ($this->getInvokeArg('displayExceptions') == true) {
            $this->view->exception = $errors->exception;
        }
        
        $this->view->request   = $errors->request;
    }

    public function getLog()
    {
        $bootstrap = $this->getInvokeArg('bootstrap');
        if (!$bootstrap->hasResource('Log')) {
            return false;
        }
        $log = $bootstrap->getResource('Log');
        return $log;
    }

    public function debugAction()
	{
		//Debug頁面的設定值。
		header("Content-Type:text/html ; charset=utf-8");
		$this->_helper->layout()->disableLayout();

		for($i=1 ; $i<23 ; $i++)
		{
//			mail('0000mox13@gmail.com', 'Cute Subject ' . $i, '<h1>Big Title:</h1>');
			
//			$ary_addTo = array('nickName' => 'Dear Mox', 'email' => '0000mox13@gmail.com');
//			$bodyInHtml = '<h1>Big Title</h1>';
//			$ary_from = array('nickName' => 'Mox\'s Local', 'email' => 'moxmox@ooxx.local');
//			$this->_byGmail($ary_addTo, 'G-mail - ' . $i, $bodyInHtml, $ary_from);
		}
	}
	
    private function _byGmail($ary_addTo, $subject, $bodyInHTML, $ary_from, $fileName=NULL, $filePath=NULL, $fileType=NULL)
    {
    	$mail = new Zend_Mail('UTF-8');
    	
    	//登入 Gmail SMTP 的必需資訊
    	$gmailconfig = array(
    		'auth'=>'login',
    		'username'=>'0000mox13@gmail.com',
    		'password'=>'XSW@ZAQ!',
    		'ssl'=>'tls',
    		'port'=>'587'
    	);
    	
        //建立一個 Zend Mail via SMTP 的物件
        $tr = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $gmailconfig);
        
    	$mail->addTo($ary_addTo['email'], $ary_addTo['nickName']);
    	
        //設為 utf-8 base 64 編碼，如此才能塞入更長的信件中文主旨而不會變成亂碼
        $mail->setSubject("=?UTF-8?B?".base64_encode($subject)."?=");
    	
    	$mail->setBodyHtml($bodyInHTML);
    	
    	$mail->setFrom($ary_from['email'], $ary_from['nickName']);
        
    	if(is_null($fileName) == FALSE || is_null($filePath) == FALSE || is_null($fileType) == FALSE)
    	{
	    	$binaryFileToAttach = file_get_contents( $filePath . '/' . $fileName);
			$at = $mail->createAttachment($binaryFileToAttach);
			$at->type = $fileType;
			$at->disposition = Zend_Mime::DISPOSITION_INLINE;
			$at->encoding = Zend_Mime::ENCODING_BASE64;
			$at->filename = $fileName;
    	}
    	
    	$mail->send($tr);
    	
    }
	


	
	
	
	
	
}

